package cn.tedu.crm.project.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.tedu.crm.orders.mapper.DrawingMapper;
import cn.tedu.crm.orders.mapper.OrdersMapper;
import cn.tedu.crm.orders.pojo.entity.Drawing;
import cn.tedu.crm.orders.pojo.entity.Orders;
import cn.tedu.crm.orders.pojo.vo.OrdersImgVO;
import cn.tedu.crm.project.mapper.ProjectMapper;
import cn.tedu.crm.project.pojo.dto.AuditParam;
import cn.tedu.crm.project.pojo.dto.ProjectAddParam;
import cn.tedu.crm.project.pojo.dto.ProjectAuditParam;
import cn.tedu.crm.project.pojo.dto.ProjectQuery;
import cn.tedu.crm.project.pojo.entity.Project;
import cn.tedu.crm.project.pojo.vo.ProjectDetailVO;
import cn.tedu.crm.project.pojo.vo.ProjectVO;
import cn.tedu.crm.project.service.ProjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ProjectServiceImpl implements ProjectService{
    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private DrawingMapper drawingMapper;

    @Override
    public List<ProjectVO> loadList(ProjectQuery query) {
        log.debug("查询工程项目业务:{}",query);
        List<ProjectVO> list = projectMapper.loadList(query);
        for (ProjectVO projectVO : list){
            Long id = projectVO.getOrdersId();
            if (id != null){
                Drawing drawing = ordersMapper.selectDrawingByOrderId(id);
                projectVO.setCreateTime(drawing.getCreateTime());
                projectVO.setUpdateTime(drawing.getUpdateTime());
            }
        }
        return list;
    }

    @Override
    public ProjectDetailVO loadDetail(Long id) {
        ProjectDetailVO projectDetailVO = new ProjectDetailVO();
        List<OrdersImgVO> ordersImgVOS = ordersMapper.loadImg(id);
        for (OrdersImgVO ordersImgVO : ordersImgVOS){
            if ("电位图".equals(ordersImgVO.getType())){
                projectDetailVO.getElectricityList().add(ordersImgVO);
            }
            if ("水位图".equals(ordersImgVO.getType())){
                projectDetailVO.getWaterList().add(ordersImgVO);
            }
        }
        String submitter = ordersImgVOS.get(0).getSubmitter();
        projectDetailVO.setSubmitName(submitter);
        Drawing drawing = ordersMapper.selectDrawingByOrderId(id);
        projectDetailVO.setSubmitDate(drawing.getCreateTime());
        return projectDetailVO;
    }

    @Override
    public void submitAudit(ProjectAuditParam param) {
        log.debug("审核图纸提交参数:{}",param);
        List<AuditParam> auditParams = param.getAuditParams();
        int i=0;
        for (AuditParam auditParam : auditParams){
            Drawing drawing = new Drawing();
            drawing.setId(auditParam.getId());
            drawing.setQualified(auditParam.getQualified());
            drawing.setUnqualified(auditParam.getUnqualified());
            drawing.setUpdateTime(new Date());
            drawing.setUpdateUser(StpUtil.getLoginIdAsLong());
            i = drawingMapper.updateById(drawing);
        }
        log.debug("图纸审核业务完成,审核的图纸总数:{}",i);
        int f = 0;
        log.debug("图纸审核业务完成,修改订单状态,若影响行数F:{}为零,则图纸审核通过",f);
        for (AuditParam auditParam : auditParams){
            Orders orders = new Orders();
            orders.setId(param.getId());
            orders.setUpdateTime(new Date());
            orders.setUpdateUser(StpUtil.getLoginIdAsLong());
            orders.setTransferProgress(30);

            if (auditParam.getQualified() != null && auditParam.getQualified() == 0){
                f = ordersMapper.update(orders);
                return;
            }
        }
        log.debug("图纸审核业务完成,修改订单状态,影响的行数F:{}",f);
        if (f == 0){
            Orders orders = new Orders();
            orders.setId(param.getId());
            orders.setUpdateTime(new Date());
            orders.setUpdateUser(StpUtil.getLoginIdAsLong());
            orders.setTransferProgress(40);
            ordersMapper.update(orders);
        }
        // 审核状态更新
        Long ordersId = param.getId();
        Project project = projectMapper.selectList(new LambdaQueryWrapper<Project>()
                        .eq(Project::getOrdersId, ordersId))
                .stream().findFirst().orElse(null);
        if (project != null) {
            project.setUpdateTime(new Date());
            project.setDrawStatus(f == 0 ? 30L : 20L);
            projectMapper.updateById(project);
            log.info("工程状态更新：ID={} 状态={}", project.getId(), project.getDrawStatus());
        } else {
            log.error("工程记录不存在，订单ID：{}", ordersId);
        }
    }

    public void addProject(ProjectAddParam param){
        log.debug("添加工程业务");
        Project project = new Project();
        project.setOrdersId(param.getOrderId());
        project.setCreateTime(new Date());
        project.setDrawStatus(10L);
        project.setUpdateTime(new Date());
        projectMapper.insert(project);
        log.debug("参数:{}",param);
    }

//    @Override
//    public List<ProjectVO> selectProject(ProjectQuery query) {
//        log.debug("查询工程项目业务:{}",query);
//        return projectMapper.selectProject(query);
//    }
}




